package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TmcPay;
import com.ruoyi.system.domain.TmcPee;
import com.ruoyi.system.mapper.TmcPeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TmcPayManageMapper;
import com.ruoyi.system.domain.TmcPayManage;
import com.ruoyi.system.service.ITmcPayManageService;

/**
 * 缴费记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-23
 */
@Service
public class TmcPayManageServiceImpl implements ITmcPayManageService {
    @Autowired
    private TmcPayManageMapper tmcPayManageMapper;
    @Autowired
    private TmcPeeMapper tmcPeeMapper;

    /**
     * 查询缴费记录列表
     *
     * @param tmcPayManage 缴费记录
     * @return 缴费记录
     */
    @Override
    public List<TmcPayManage> selectTmcPayManageList(TmcPayManage tmcPayManage) {
        return tmcPayManageMapper.selectTmcPayManageList(tmcPayManage);
    }

    /**
     * 新增缴费记录
     *
     * @param tmcPayManage 缴费记录
     * @return 结果
     */
    @Override
    public int insertTmcPayManage(TmcPayManage tmcPayManage) {
        tmcPayManage.setCreateTime(DateUtils.getNowDate());
        return tmcPayManageMapper.insertTmcPayManage(tmcPayManage);
    }

    /**
     * 修改缴费记录
     *
     * @param tmcPayManage 缴费记录
     * @return 结果
     */
    @Override
    public int updateTmcPayManage(TmcPay tmcPayManage) {
        TmcPay tmcPayManage1 = new TmcPay();
        Long id = tmcPeeMapper.queryCompanyByid(tmcPayManage.getCompanyName());
//        获取小区计费方式
        TmcPee tmcPee = tmcPeeMapper.getPee(Integer.valueOf(id+""));
        //电费
        Double sum =tmcPayManage.getSum();
        if (0 != tmcPayManage.getWater() && tmcPayManage.getWater() != 0) {
            sum-=tmcPayManage.getElectricity();
            tmcPayManage1.setElectricity(Double.valueOf((tmcPayManage.getElectricity() * tmcPee.getElectricity())+""));
            sum+=tmcPayManage.getElectricity();
        }
        if (0 != tmcPayManage.getElectricity() && tmcPayManage.getElectricity() != 0) {
            //水费
            sum-=tmcPayManage.getWater();
            tmcPayManage1.setWater(Double.valueOf((tmcPayManage.getWater() * tmcPee.getWater())+""));
            sum+=tmcPayManage.getWater();
        }
        tmcPayManage1.setSum(Double.valueOf((sum+"")));
        tmcPayManage1.setId(tmcPayManage.getId());
        return tmcPayManageMapper.updateTmcPayManage(tmcPayManage1);
    }

    @Override
    public TmcPayManage queryById(Long id) {
        return tmcPayManageMapper.queryById(id);
    }

}
